package org.lib.postgresqlhello.device;

import org.lib.postgresqlhello.device.dto.AddDeviceDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value="/api/device")
@Slf4j
public class DeviceContoller {

	@Autowired
	private DeviceMapper dm;
	
	/**
	 * 添加一个设备
	 * @author ben
	 * @date 2021-11-08 21:06:30 CST
	 * @param dto
	 * @return 成功-返回设备id，否则，返回null
	 */
	@PostMapping(value="/add")
	Long addOne(@RequestBody AddDeviceDTO dto) {
		log.info("AddDeviceDTO dto={}", dto);
		Device nd = dto.convertToDevice();
		long ret = dm.addOne(nd);
		log.info("Device nd={}, ret={}", nd, ret);
		
//		nd = dto.convertToDevice();
//		nd.setName(nd.getName() + 1);
//		ret = dm.addOne1(nd);
//		log.info("addOne1 Device nd={}, ret={}", nd, ret);
//
//		nd = dto.convertToDevice();
//		nd.setName(nd.getName() + 2);
//		ret = dm.addOne2(nd);
//		log.info("addOne2 Device nd={}, ret={}", nd, ret);
//		
//		nd = dto.convertToDevice();
//		nd.setName(nd.getName() + 3);
//		ret = dm.addOne3(nd);
//		log.info("addOne3 Device nd={}, ret={}", nd, ret);
//		
//		nd = dto.convertToDevice();
//		nd.setName(nd.getName() + 4);
//		ret = dm.addOne4(nd);
//		log.info("addOne4 Device nd={}, ret={}", nd, ret);
		
		// 注解式
		
//		nd = dto.convertToDevice();
//		nd.setName(nd.getName() + 4);
//		ret = dm.addOne5(nd);
//		log.info("addOne5 Device nd={}, ret={}", nd, ret);
//		
//		nd = dto.convertToDevice();
//		nd.setName(nd.getName() + 4);
//		ret = dm.addOne6(nd);
//		log.info("addOne6 Device nd={}, ret={}", nd, ret);
//		
//		nd = dto.convertToDevice();
//		nd.setName(nd.getName() + 4);
//		ret = dm.addOne7(nd);
//		log.info("addOne7 Device nd={}, ret={}", nd, ret);

		nd = dto.convertToDevice();
		nd.setName(nd.getName() + 4);
		ret = dm.addOne8(nd);
		log.info("addOne8 Device nd={}, ret={}", nd, ret);

		nd = dto.convertToDevice();
		nd.setName(nd.getName() + 4);
		ret = dm.addOne9(nd);
		log.info("addOne9 Device nd={}, ret={}", nd, ret);
		
		// 一直是1 TODO
//		return ret;
		// 返回id
		return nd.getId();
	}
	
	@PostMapping(value="/updateById/3")
	Long updateById3(@RequestParam Long id, @RequestParam(required = false) Long sn, @RequestParam(required = false) String name) {
		Long ret = dm.updateById3(id, sn, name);
		log.info("updateById3 ret={}", ret);
		return ret;
	}
	
	@DeleteMapping(value="/deleteBySn")
	Long deleteBySn(@RequestParam Long sn) {
		Long ret = dm.deleteBySn(sn);
		log.info("deleteBySn ret={}", ret);
		return ret;
	}
	
}
